package com.hbsx.why.ui.home;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.hbsx.why.R;
import com.hbsx.why.adapter.ActivityListAdapter;
import com.hbsx.why.base.BaseActivity;
import com.hbsx.why.utils.LogUtils;
import com.hbsx.why.utils.PopMenu;
import com.hbsx.why.utils.refresh.PullListView;
import com.hbsx.why.utils.refresh.PullToRefreshLayout;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by User on 2017/8/29.
 */

public class ActivityCalendar extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.tv_topTitle)
    TextView tvTopTitle;
    @BindView(R.id.activity_calendarView)
    MaterialCalendarView activityCalendarView;
    @BindView(R.id.activitycalendarView_PullListView)
    PullListView activitycalendarViewPullListView;
    @BindView(R.id.activitycalendarView_PullToRefreshLayout)
    PullToRefreshLayout activitycalendarViewPullToRefreshLayout;

    private Context context = this;
    private List<String> stringList, flagString;
    private int page = 0;
    private ActivityListAdapter listAdapter;
    OnFrgDataListener IListener;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_home_activitycalender;
    }

    @Override
    protected void initView() {
        tvTopTitle.setText("活动日历");
        activityCalendarView.setSelectedDate(new Date());
//                // 设置你的日历的最小的月份  月份为你设置的最小月份的下个月 比如 你设置最小为1月 其实你只能滑到2月
//                .setMinimumDate(CalendarDay.from(2015, 1, 1))
//                // 同最小 设置最大
//                .setMaximumDate(CalendarDay.from(2018, 1, 1));
        //      设置点击日期的监听
        activityCalendarView.setOnDateChangedListener(new OnDateSelectedListener() {
            /**
             * 日期选择 回调函数
             *
             * @param widget
             * @param date
             * @param selected
             */
            @Override
            public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
                LoadNewData();
//                Toast.makeText(context, getSelectedDatesString().substring(8, 9), Toast.LENGTH_SHORT).show();
                //       Toast.makeText(getActivity(),"从Activity传递的数据："+(String)timeBundle.get("date"),Toast.LENGTH_SHORT).show();
//                IListener.progress(activityCalendarView.getSelectedDate().getDate());

                //选择后,延迟消失
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
//                mContext.dismiss();
                    }
                }, 1000);
            }
        });
        LoadNewData();

    }

    private void LoadNewData() {
        stringList = new ArrayList<>();
        listAdapter = new ActivityListAdapter(context, stringList);
        activitycalendarViewPullListView.setAdapter(listAdapter);
        page = 1;
        getData(page);
        activitycalendarViewPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        getData(page);
                        activitycalendarViewPullToRefreshLayout.refreshFinish(true);
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = page + 1;
                        getData(page);
                        activitycalendarViewPullToRefreshLayout.loadMoreFinish(true);
                    }
                }, 1000);
            }
        });
    }


    /**
     * 将日期转换为字符串
     * 2017年8月29日
     *
     * @return
     */
    private String getSelectedDatesString() {
        CalendarDay date = activityCalendarView.getSelectedDate();
        if (date == null) {
            return "No Selection";
        }
        Log.i("sinstar", "getSelectedDatesString: " + date.toString());
        return FORMATTER.format(date.getDate());
    }

    private static final DateFormat FORMATTER = SimpleDateFormat.getDateInstance();

    /**
     * 定义接口，向Activity传递数据
     */
    public interface OnFrgDataListener {
        public void progress(Date date);
    }

    /**
     * 数据加载
     *
     * @param page
     */
    private void getData(int page) {
        int a = 0;
        if (page == 1) {
            stringList = new ArrayList<>();
            a = Integer.parseInt((getSelectedDatesString().replace("日", "0")).substring(8, 9));
            for (int i = a; i < 10; i++) {
                if (i % 5 == 0) {
                    stringList.add("潜山县鼓书琴书参加安庆市展演");
                }
                if (i % 5 == 1) {
                    stringList.add("天天演 天天笑 天天乐");
                }
                if (i % 5 == 2) {
                    stringList.add("彭保友民俗花钱收藏展");
                }
                if (i % 5 == 3) {
                    stringList.add("第三届相声大会");
                }
                if (i % 5 == 4) {
                    stringList.add("姚道馀写生作品展");
                }

            }
//            LogUtils.loge("------->" + stringList.get(1));
            listAdapter.loadMoreListView(stringList);
            if (stringList.size() < 1) {
                activitycalendarViewPullListView.setPullUpEnable(false);
            } else {
                activitycalendarViewPullListView.setPullUpEnable(true);
            }
        } else {
            stringList = new ArrayList<>();
            for (int i = a; i < 10; i++) {
                if (i % 5 == 0) {
                    stringList.add("“猴年说猴”趣味故事会");
                }
                if (i % 5 == 1) {
                    stringList.add("文化惠民春潮涌");
                }
                if (i % 5 == 2) {
                    stringList.add("市书画院精心打造“廉政文化长廊”");
                }
                if (i % 5 == 3) {
                    stringList.add("书画艺术进军营，军民共叙强军梦");
                }
                if (i % 5 == 4) {
                    stringList.add("新火车站笔友会");
                }
            }
            listAdapter.loadMoreListView(stringList);
        }
        listAdapter.notifyDataSetChanged();
    }


    @Override
    public void onClick(View v) {

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
